local skel = fk.CreateSkill({
  name = "kpaI__xiangji",
  tags = { Skill.Combo },
})

Fk:loadTranslationTable{
  ["kpaI__xiangji"] = "相计",
  [":kpaI__xiangji"] = "连招技（锦囊牌+锦囊牌），你“涅槃”；且直到你下次破招，你始终横置。",

  ["@@kpaI__xiangji"] = "连招：[锦囊牌]+",
  ["@@kpaI__xiangji_end"] = "相计",

  ["$kpaI__xiangji1"] = "（连招）我有一计，管教刘璋拱手来降。",
  ["$kpaI__xiangji2"] = "（连招）我有一计，可使主公大展鸿图。",
  ["$kpaI__xiangji3"] = "（成功）当下春风正好，君可扶摇而上。",
  ["$kpaI__xiangji4"] = "（失败）折纸为甲，可避刀兵。",
  ["$kpaI__xiangji5"] = "（横置）牡丹胜火，锦绣共存。",

}

skel:addEffect(fk.CardUseFinished,{
  mute = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.from and data.from == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@@kpaI__xiangji") > 0 then
      --连招中
      if data.card.type == Card.TypeTrick then
        --成功
        player.room:notifySkillInvoked(player, skel.name)
        player:broadcastSkillInvoke(skel.name,3)
        room:setPlayerMark(player,"@@kpaI__xiangji",0)
        --涅槃
        player:throwAllCards("hej", skel.name)
        if player.dead then return false end
        player:reset()
        if player.dead then return false end
        player:drawCards(3, skel.name)
        if player.dead then return false end
        if player:isWounded() then
          room:recover{
            who = player,
            num = math.min(3, player.maxHp) - player.hp,
            recoverBy = player,
            skillName = skel.name,
          }
        end
        --涅槃
        if not player.chained then
          player:setChainState(true)
        end
        room:setPlayerMark(player,"@@kpaI__xiangji_end",1)
      else
        --失败
        player.room:notifySkillInvoked(player, skel.name)
        player:broadcastSkillInvoke(skel.name,4)
        room:setPlayerMark(player,"@@kpaI__xiangji",0)
        room:setPlayerMark(player,"@@kpaI__xiangji_end",0)
        player:setChainState(false)
      end
    else
      --开始连招
      player.room:notifySkillInvoked(player, skel.name)
      player:broadcastSkillInvoke(skel.name,math.random(1,2))
      if data.card.type == Card.TypeTrick then
        room:setPlayerMark(player,"@@kpaI__xiangji",1)
      end
    end
  end
})

skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player,"@@kpaI__xiangji",0)
  room:setPlayerMark(player,"@@kpaI__xiangji_end",0)
  player:setChainState(false)
end)

skel:addEffect(fk.BeforeChainStateChange, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getMark("@@kpaI__xiangji_end") > 0 and player.chained
  end,
  on_cost =function (self, event, target, player, data)
    return true
  end,
  on_use = function (self, event, target, player, data)
    player.room:notifySkillInvoked(player, skel.name)
    player:broadcastSkillInvoke(skel.name,5)
    data.prevented = true
  end,
})


return skel
